-- This file is hereby placed in the Public Domain
local addonName, ns = ...
local addon = ns[addonName]
local oUF = ns.oUF

local utils = {}
addon.utils = utils

local format = string.format

utils.path = format([[Interface\AddOns\%s\]], addonName)
utils.mediaPath = utils.path .. 'Textures\\'
utils.isCwow = GetLocale() == 'zhCN' and (select(4, GetBuildInfo()) < 30300)

utils.backdrop = {
	bgFile = [=[Interface\ChatFrame\ChatFrameBackground]=],
	insets = {top = -1, left = -1, bottom = -1, right = -1},
}

function utils.adjustCoord(bool, texture)
    if(bool and texture.SetTexCoord) then
        texture:SetTexCoord(1,0,0,1)
    end
end

function utils.utf8sub(str, num)
	local i = 1
	while num > 0 and i <= #str do
		local c = strbyte(str, i)
		if(c >= 0 and c <= 127) then
			i = i + 1
		elseif(c >= 194 and c <= 223) then
			i = i + 2
		elseif(c >= 224 and c <= 239) then
			i = i + 3
		elseif(c >= 240 and c <= 224) then
			i = i + 4
		end
		num = num - 1
	end

	return str:sub(1, i - 1)
end

function utils.truncate(value)
	if(value >= 1e6) then
		value = format('%.1fm', value / 1e6)
	elseif(value >= 1e3) then
		value = format('%.1fk', value / 1e3)
	end
	return gsub(value, '%.?0+([km])$', '%1')
end

function utils.hex(r, g, b)
	if(type(r) == 'table') then
		if(r.r) then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	
	if(not r or not g or not b) then
		r, g, b = 1, 1, 1
	end
	
	return format('|cff%02x%02x%02x', r*255, g*255, b*255)
end

function utils.setSize(f, w, h)
    if(f and f.SetWidth and f.SetHeight and w) then
        f:SetWidth(w)
        f:SetHeight(h or w)
    end
end

function utils.testBackdrop(f)
    f:SetBackdrop(utils.backdrop)
    --f:SetBackdropColor(0,0,0, .5)
    f:SetBackdropColor(1,0,0,.5)
end


function utils.reverseTexture(tex)
    tex:SetTexCoord(1,0,0,1)
end

function utils.ShortValue(value)
	if(value >= 1e6) then
		return ('%.2fm'):format(value / 1e6):gsub('%.?0+([km])$', '%1')
	elseif(value >= 1e4) then
		return ('%.1fk'):format(value / 1e3):gsub('%.?0+([km])$', '%1')
	else
		return value
	end
     end

function utils.utf8sub(string, i, dots)
	local bytes = string:len()
	if (bytes <= i) then
		return string
	else
		local len, pos = 0, 1
		while(pos <= bytes) do
			len = len + 1
			local c = string:byte(pos)
			if (c > 0 and c <= 127) then
				pos = pos + 1
			elseif (c >= 192 and c <= 223) then
				pos = pos + 2
			elseif (c >= 224 and c <= 239) then
				pos = pos + 3
			elseif (c >= 240 and c <= 247) then
				pos = pos + 4
			end
			if (len == i) then break end
		end

		if (len == i and pos <= bytes) then
			return string:sub(1, pos - 1)..(dots and '...' or '')
		else
			return string
		end
	end
     end

----------------------castbartime------------------------
function utils.castbarTime(self, duration)
	if self.casting then
		self.Time:SetFormattedText('%.1f ', self.max - duration)
	elseif self.channeling then
		self.Time:SetFormattedText('%.1f - %.1f', duration, self.max)
	end
     end

-------------Time format-----------------
function utils.FormatTime(s)
	if s <= 60 then
		return format("%.1f", s), s - floor(s)
	elseif s <= 300 then
		return format('%d:%02d', floor(s/60), s % 60), s - floor(s)
	elseif s <= 3600 then
		return format("%dm", floor(s/60 + 0.5)), s % 60
	else -- hour
		return format("%dh", floor(s/3600 + 0.5)), s % 3600
	end
     end